package com.ssfGame.puremvc.view.module.Game
{
	import api.event.CustomEvent;
	
	import com.ssfGame.puremvc.StaticConst;
	import com.ssfGame.puremvc.view.application.SceneLayerMediator;
	import com.ssfGame.puremvc.view.module.Game.vc.GameVC;
	import com.ssfGame.puremvc.view.module.ModuleVC;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class GameMediator extends Mediator
	{
		public static const NAME:String="GameMediator";
		private var _gameVC:GameVC;
		public function GameMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		override public function listNotificationInterests():Array{
			return [
				StaticConst.N_M_GAME_ADD,
				StaticConst.N_M_GAME_DEL
			]
		}
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case StaticConst.N_M_GAME_ADD:
					sendNotification(StaticConst.N_M_SCENE_LAYER_DEL,null,SceneLayerMediator.DEL_ALL);
					if(_gameVC==null){
						_gameVC=new GameVC();
						_gameVC.addEventListener(ModuleVC.TO_MEDIATOR,recEvent);
					}
					sendNotification(StaticConst.N_M_SCENE_LAYER_ADD,_gameVC);
					break;
				case StaticConst.N_M_GAME_DEL:
					delGameVC();
					break;
			}
		}
		private function recEvent(e:CustomEvent):void{
			switch(e.type){
				case GameVC.USER_CHANGE:
					delGameVC();
					sendNotification(StaticConst.N_C_INIT_PVP_API);;
					break
				case GameVC.GAME_OVER:
					delGameVC();
					sendNotification(StaticConst.N_M_GAME_OVER_ADD);;
					break
			}
		}
		private function delGameVC():void{
			if(_gameVC!=null){
				sendNotification(StaticConst.N_M_SCENE_LAYER_DEL,_gameVC);
				_gameVC.removeEventListener(ModuleVC.TO_MEDIATOR,recEvent);
				_gameVC.gc();
				_gameVC=null;
			}
		}
		
	}
}